package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.makeunion.gdlbsdemo.R;

/**
 * <p>write the description
 *
 * @author renjialiang
 * @version [版本号]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class FourTypeMapActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    private LinearLayout trafficLayout;

    private CheckBox cbTraffic;

    private CheckBox cbBuilding;

    private CheckBox cbMapText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four_type_map);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        trafficLayout = (LinearLayout)findViewById(R.id.traffic_layout);

        cbTraffic = (CheckBox)findViewById(R.id.cb_traffic);
        cbBuilding = (CheckBox)findViewById(R.id.cb_building);
        cbMapText = (CheckBox)findViewById(R.id.cb_map_text);

        cbTraffic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onChecked(0);
            }
        });
        cbBuilding.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onChecked(1);
            }
        });
        cbMapText.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onChecked(2);
            }
        });
        init();
    }

    private void onChecked(int index) {
        switch (index) {
            case 0:
                aMap.setTrafficEnabled(cbTraffic.isChecked());
                break;
            case 1:
                aMap.showBuildings(cbBuilding.isChecked());
                break;
            case 2:
                aMap.showMapText(cbMapText.isChecked());
                break;
            default:
                break;
        }
    }

    public void standardMap(View view) {
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
        onTypeClick(0);
    }

    public void satelliteMap(View view) {
        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式
        onTypeClick(1);
    }

    public void nightMap(View view) {
        aMap.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图模式
        onTypeClick(2);
    }

    public void naviMap(View view) {
        aMap.setMapType(AMap.MAP_TYPE_NAVI);//导航地图模式
        onTypeClick(3);
    }

    private void onTypeClick(int index) {
        switch (index) {
            case 0:
            case 1:
            case 2:
                trafficLayout.setVisibility(View.GONE);
                cbTraffic.setChecked(false);
                cbBuilding.setChecked(false);
                cbMapText.setChecked(false);
                break;
            case 3:
                trafficLayout.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
        aMap.setTrafficEnabled(false);
        aMap.showBuildings(false);
        aMap.showMapText(false);
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
